package com.ifi.app.photofun;


import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private static final String  TAG = "PhotoFun::Main::Activity";
	
	ImageProcessor imageProcessor;
	
	int 	typeEdit = Info.DEFORMER;
	Button 	btnGallery;
	Button 	btnFaceEffect;
	Button 	btnDeformer;
	Button 	btnSopiaColor;
	ImageView imageView;
//	Bitmap image;
	boolean isImageClick = true;
	boolean isLiquify = true;
	
//	Mat mat;
	
//	static {
//	    if (!OpenCVLoader.initDebug()) {
//	        // Handle initialization error
//	    }
//	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i(TAG, "onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		addListenerOnButton();

		Log.i(TAG, "Opencv Mat");
		
//		mat = new Mat();
		imageProcessor = new ImageProcessor();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void addListenerOnButton() {

		imageView = (ImageView) findViewById(R.id.image_view);
		//On touch
		imageView.setOnTouchListener(new OnTouchListener() 
        {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				
				Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
//System.out.println("Main activity, x: " + v.getScaleX() + " y: "+ v.getScaleY());
				if(isLiquify){
					//applied process
					bitmap = ImageProcessor.liquify(bitmap, (int)event.getX(), (int)event.getY(), 100.0);
				}else{
					//applied process
					bitmap = ImageProcessor.doFishEye(bitmap, (int)event.getX(), (int)event.getY(), 80.0);
				}
				
				// set to imageView
				imageView.setImageBitmap(bitmap);
				
				Toast.makeText(MainActivity.this,
						"On touch! x= " + event.getX() + " y = " + event.getY(), Toast.LENGTH_SHORT).show();
				
				return false;
			}
        });
		
		
		
		// gallery
		btnGallery = (Button) findViewById(R.id.gallery_button);
		btnGallery.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				
				chooseGallery(view);
			}
		});

		// face effect
		btnFaceEffect = (Button) findViewById(R.id.face_effect_button);
		btnFaceEffect.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				if (isImageClick) {
					imageView.setImageResource(R.drawable.tap);
					isImageClick = false;
				} else {
					imageView.setImageResource(R.drawable.people);
					isImageClick = true;
				}
			}
		});

		// deformation
		btnDeformer = (Button) findViewById(R.id.deformer_button);
		btnDeformer.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				
				if (isLiquify) {isLiquify = false;}else isLiquify = true;
//				Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
				
				//applied process
//				bitmap = ImageProcessor.createSepiaToningEffect(bitmap, 3, 0, 10, 9);
//				Bitmap bitmap1 = ImageProcessor.doFishEye(bitmap);
				
//				bitmap = ImageProcessor.doFishEye(bitmap);//morphStage(bitmap, bitmap1, 3, 2);
				// set to imageView
//				imageView.setImageBitmap(bitmap);
				
				Toast.makeText(MainActivity.this, "Deformer!",
						Toast.LENGTH_SHORT).show();
				
			}
		});
		
		// color
		btnSopiaColor = (Button) findViewById(R.id.sopia_color);
		btnSopiaColor.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
				// applied process
				bitmap = ImageProcessor.createSepiaToningEffect(bitmap, 3, 0, 10, 9);
				// set to imageView
				imageView.setImageBitmap(bitmap);
				Toast.makeText(MainActivity.this, "Sopia Color!",
						Toast.LENGTH_SHORT).show();

			}
		});
	}

	// Select picture auto create
	private int SELECT_PICTURE;

	public void chooseGallery(View view) {
		Intent intent = new Intent();
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
		startActivityForResult(Intent.createChooser(intent, "SelectPicture"),
				SELECT_PICTURE);
	}

	//
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			if (requestCode == SELECT_PICTURE) {
				imageView = (ImageView) findViewById(R.id.image_view);
				Uri selectedImageUri = data.getData();
				imageView.setImageURI(selectedImageUri);
			}
		}
	}
}